package com.yiwyn.demo.service;

import com.yiwyn.demo.scanner.AnnotationScanner;
import com.yiwyn.demo.scanner.TaskMethodScanner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@Slf4j
@Service
public class TaskService {

    // @Autowired
    // AnnotationScanner annotationScanner;

    @Autowired
    private TaskMethodScanner annotationScanner;

    public String runTask(int apiCode) {
        Map<Integer, AnnotationScanner.TaskNode> methodNames = annotationScanner.getMethodNames();
        AnnotationScanner.TaskNode taskNode = methodNames.get(apiCode);

        String param = "我是一个固定参数";
        Object bean = taskNode.getBean();
        Boolean hasParam = taskNode.getHasParam();
        try {
            if (hasParam) {
                taskNode.getMethod().invoke(bean, param);
            } else {
                taskNode.getMethod().invoke(bean);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }

        return "";
    }
}
